package com.ajr.core;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.jnr.servlets.SubmitOrder;

import se.codevision.jinterlink.JInterLink;
import se.codevision.jinterlink.JInterLinkException;
import se.codevision.jinterlink.registers.Article;
import se.codevision.jinterlink.registers.JInterLinkRegisterFactory;

public class ArticleAction {
	Logger logger = Logger.getLogger(ArticleAction.class);

	public static final int ARTICLIE_NAME_LIMIT = 30;

	
	public void insertArticle(Vector vector) throws JInterLinkException {
		Enumeration enumeration;
		Article article;
		enumeration = vector.elements();
		article = null;
		do {
			if (!enumeration.hasMoreElements())
				break;
			article = JInterLinkRegisterFactory.createArticle();
			
			ArticleObject articleObject = (ArticleObject) enumeration
					.nextElement();
			String aticleNumber = articleObject.getNumber();
			if (aticleNumber != null && aticleNumber.length() > 0)// !k.C() &&
			{
//				logger.info("articleObject.getNumber() ["
//						+ articleObject.getNumber() + "]"
//						+ articleObject.getName());
				article.setNumber(String.valueOf(articleObject.getNumber()));
				boolean flag = article.find();
				if (flag&& !article.getNumber().equals(
								String.valueOf(articleObject.getNumber()))) {
					flag = false;
				}else {
					logger.info("Article Price ## "+article.getPrice());
					article.reset();
					article.setNumber(String.valueOf(articleObject.getNumber()));
				}
				// flag = false;
				String articleName = articleObject.getName();
				if (articleName != null && articleName.length() > ARTICLIE_NAME_LIMIT)
					articleName = articleName.substring(0, ARTICLIE_NAME_LIMIT);
				double d = articleObject.getPrice(); // k.G()
				d = getPriceIncludingVatAction(d, articleObject.getVatValue());
				/*if (Settings.getInstance().isIncludeVat4Invoice()) {
					String vatMode = Settings.getInstance().getVatAction();
					if(vatMode.equalsIgnoreCase("add")) {
						d *= articleObject.getVatValue() / 100D + 1.0D;// k.B()
					}
					if(vatMode.equalsIgnoreCase("subtract")) {
						d *= 1.0D - articleObject.getVatValue() / 100D;// k.B()
					}
				}*/
				BigDecimal bigdecimal = new BigDecimal(d);
				bigdecimal = bigdecimal.setScale(2, 4);
				article.setNumber(String.valueOf(aticleNumber));
//				if(flag)
				logger.info("Article Price ** "+bigdecimal.doubleValue());
				
				article.setPrice(bigdecimal.doubleValue());
				article.setName(articleName);
				
//				article.setBool(false);
				boolean webshop = Settings.getInstance().getStockType().equals("webshop");
				if(webshop) {
					article.setBool(Article.ADK_ARTICLE_WEBSHOP,true);
				}
//				article.setBool(Article.ADK_ARTICLE_STOCK_VALUE_INACTIVE,true);
//				article.setBool(Article.ADK_ARTICLE_STOCK_VALUE,false);
				boolean stockType = Settings.getInstance().getStockType().equals("stock");
				article.setBool(Article.ADK_ARTICLE_STOCK_GOODS,stockType);
				
				
				
				
				
				if(false && articleObject.getLastModified() != null) {
//					logger.info("Setting last modified "+articleObject.getLastModified());
					article.setDate(Article.ADK_ARTICLE_INVENTORY_DATE, articleObject.getLastModified());
//					article.setDate(1, articleObject.getDatePurchased());
				}else {
//					logger.info("Setting last modified new date");
					article.setDate(Article.ADK_ARTICLE_INVENTORY_DATE, new Date());
//					article.setDate(Article.ADK_ARTICLE_INVOICE_DATE, articleObject.getLastModified());
//					article.setDate(arg0, arg1);
//					article.setDate(2, new Date());
				}
				// article.set

				// article.set
				

				article.setPriceList(Settings.getInstance().getPriceListCode());
				if(Settings.getInstance().isSynchronizeArticleInformation()) {
					if (!flag) {
						logger.info("inserting article");
						JInterLink.getInstance().add(article);
						
					}else { 
						//article.updateRecord();
						logger.info("article alredy exists");
						
					}
				}
				if (article != null)
					article.release();
				
			}
		} while (true);
		
	}
	
	public static double getPriceIncludingVatAction(double price,double vatValue) {
		String vatMode = Settings.getInstance().getVatAction();
		
		if(vatMode.equalsIgnoreCase("subtract")) {
			double t = price/(vatValue / 100D + 1.0D); // k1.B()
			return t;
		}
		if(vatMode.equalsIgnoreCase("add")) {
			double t = price*(1.0D + vatValue / 100D); // k1.B()
			return t;
		}
		return price;
	}

	
	public static void main(String[] args) throws JInterLinkException {
		
		String system = Settings.getInstance().getSystemPath();
		String company =  Settings.getInstance().getCompanyPath();
		String s =  Settings.getInstance().getLibraryPath();

		
		JInterLink j = JInterLink.getInstance();
		try {

			j.open(s, system, company);

			ArticleAction articleArt = new ArticleAction();
			Vector v = new Vector();
			
			ArticleObject articleObject = new ArticleObject();
			
			articleObject.setName("Jayaramajayaramreddygaruisbesoneinwordistakjdsfsafwieriwjelkfjskdfjsdfiwiejrlejis");
			articleObject.setNumber("A17471");
			articleObject.setPrice(799.0);
			
			v.add(articleObject);
			
			articleArt.insertArticle(v);
			
			System.out.println("inserted article");
			
		}catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		j.close();
		
	}
}
